package cn.wangdh.domain.strategy.service.rule.chain.factory;

import cn.wangdh.domain.strategy.model.entity.StrategyEntity;
import cn.wangdh.domain.strategy.repository.IStrategyRepository;
import cn.wangdh.domain.strategy.service.rule.chain.ILogicChain;
import lombok.*;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author WDH
 * @description
 * @date 2024年10月21日 14:35
 */

@Service
public class DefaultChainFactory {

//这里ILogicChain的三个实现类被@Component所注入，只要你用这个map，string就是他的注入名，ILogicChain就是对应的实现类
    private  Map<String, ILogicChain> logicChainGroup;


    private IStrategyRepository repository;

    public DefaultChainFactory(Map<String, ILogicChain> logicChainGroup, IStrategyRepository repository) {
        this.logicChainGroup = logicChainGroup;
        this.repository = repository;
    }


    //配置责任链
    public ILogicChain openLogicChain(Long strategyId) {
        StrategyEntity strategy = repository.queryStrategyEntityByStrategyId(strategyId);
        String[] ruleModels = strategy.ruleModels();
        if (ruleModels == null || ruleModels.length == 0) return logicChainGroup.get("default");
        ILogicChain logicChain = logicChainGroup.get(ruleModels[0]);
        //这是指针
        ILogicChain current = logicChain;
        for (int i = 1; i < ruleModels.length; i++) {
            ILogicChain nextChain = logicChainGroup.get(ruleModels[i]);
            current = current.appendNext(nextChain);
        }
        current.appendNext(logicChainGroup.get("default"));
        return logicChain;
    }


    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class StrategyAwardVO{
        private Integer awardId;
        private String logicModel;
    }

    @Getter
    @AllArgsConstructor
    public enum LogicModel{
        //也叫做兜底奖励
        RULE_DEFAULT("rule_default","默认抽奖"),
        RULE_BLACKLIST("rule_blacklist","黑名单抽奖"),
        RULE_WEIGHT("rule_weight","权重规则"),
        ;

        private final String code;
        private final String info;

    }

}
